package com.github.binarywang.demo.wx.mp.service;

import com.github.binarywang.demo.wx.mp.beans.HaoZhu;
import com.github.binarywang.demo.wx.mp.dto.HaoZhuIdProccessor;
import com.github.binarywang.demo.wx.mp.dto.Result;
import com.github.binarywang.demo.wx.mp.jwt.JwtUtil;
import com.github.binarywang.demo.wx.mp.mapper.Haozhumapper;
import com.github.binarywang.demo.wx.mp.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HaoZhuserviceImpl implements Haozhuservice{
    @Autowired
    private Haozhumapper hm;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public List<HaoZhu> getAllByrole(String role) {
        return hm.getAllByrole(role);
    }

    @Override
    public int countHZNumByrole(String role) {
        return hm.countHZNumByrole(role);
    }


    @Override
    public void selectByName(String Name) {
        hm.selectByName(Name);
    }


    @Override
    public Result registerHZ(HaoZhu haoZhu) {

        try {
            HaoZhu existUser = hm.selectByName(haoZhu.getUsername());

            if (haoZhu.getUsername().equals("")){
                return Result.error("账户名不能为空");
            }else if (haoZhu.getPassword().equals("")){
                return Result.error("密码不能为空");
            }else if (existUser != null){
                return Result.error("用户名已存在");
            }else if (haoZhu.getTelphone().equals("")){
                return Result.error("手机号不能为空");
            }else if (haoZhu.getCheckcode().equals("")){
                return Result.error("验证码不能为空");
            }else if(!haoZhu.getPassword().equals(haoZhu.getCheckpassword())){
                return Result.error("密码和确认密码不一致");
            }else if(!redisTemplate.opsForValue().get(haoZhu.getTelphone()).equals(haoZhu.getCheckcode())){
                return Result.error("验证码错误");
            }else if(haoZhu.getRole().equals("")){
                return Result.error("角色不能为空");
            }else {
                haoZhu.setHao_zhu_id(HaoZhuIdProccessor.getInstance().makeHaoZhuId());
                haoZhu.setTimes(new Date());
                haoZhu.setPassword(Md5Utils.inputPassToFormPass(haoZhu.getPassword()));
                hm.registerHZ(haoZhu);
                return Result.success(haoZhu);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    @Override
    public Result loginHZ(HaoZhu haoZhu) {

        try {
            String username = haoZhu.getUsername();
            String password = haoZhu.getPassword();
            //调用MD5工具类进行加密
            String password2=Md5Utils.inputPassToFormPass(password);
            // 省略 账号密码验证
            // 验证成功后发送token
            String token = JwtUtil.sign(username,password);

            if (username.equals("")){
                return Result.error("账户名不能为空");
            }else if (password.equals("")){
                return Result.error("密码不能为空");
            }
            HaoZhu userExistN = hm.selectByName(username);
            if (userExistN != null){
                String userExistP = hm.findPswByName(username);
                if (userExistP.equals(password2)){
                    haoZhu.setPassword(password2);
                    HaoZhu hz=hm.loginHZ(haoZhu);
                    Map<String, Object> map = new HashMap<>();
                    map.put("token",token);
                    map.put("haozhu",hz);

                    return Result.success(map);
                }else {
                    return Result.error("登陆失败,密码错误！");
                }
            }else{
                return Result.error("登陆失败，账户不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());

        }


    }

    @Override
    public void deleteHZById(String HaoZhuId) {
        hm.deleteHZById(HaoZhuId);
    }

    @Override
    public void updateHZById(HaoZhu haoZhu) {
        hm.updateHZById(haoZhu);
    }


    @Override
    public String findPswByName(String username) {
        return hm.findPswByName(username);
    }

    @Override
    public Long getIdbyhaozhuid(String hao_zhu_id) {
        return hm.getIdbyhaozhuid(hao_zhu_id);
    }
}
